package com.zy.modules.sys.service.impl;

import com.zy.modules.sys.entity.SysUserEntity;
import com.zy.modules.sys.repo.UserDao;
import com.zy.modules.sys.repo.UserRepository;
import com.zy.modules.sys.service.SysUserRoleService;
import com.zy.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Resource
	private UserRepository userRepository;
	@Resource
	private UserDao userDao;

	@Override
	public List<String> queryAllPerms(Long userId) {
		return userDao.queryAllPerms(userId);
	}

	@Override
	public List<Long> queryAllMenuId(Long userId) {
		return userDao.queryAllMenuId(userId);
	}

	@Override
	public SysUserEntity queryByUserName(String username) {
		return userRepository.findByUsername(username);
	}
	
	@Override
	public SysUserEntity queryObject(Long userId) {
		return userRepository.findOne(userId);
	}

	@Override
	public List<SysUserEntity> queryList(Map<String, Object> map){
		int limit = (int) map.getOrDefault("limit", 10);
		int offset = (int) map.getOrDefault("offset", 0);
		Pageable pageable = new PageRequest(offset, limit);
		Page<SysUserEntity> entities = userRepository.findAll(pageable);
		return entities.getContent();
	}
	
	@Override
	public int queryTotal(Map<String, Object> map) {
		long count = userRepository.count();
		return (int) count;
	}

	@Override
	@Transactional
	public void save(SysUserEntity user) {
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
		user.setSalt(salt);
		userRepository.save(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
		}
		userRepository.save(user);

		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	@Transactional
	public void deleteBatch(Long[] userIds) {
		for (Long userId : userIds) {
			userRepository.delete(userId);
		}
	}

	@Override
	public int updatePassword(Long userId, String password, String newPassword) {
		return userRepository.updatePassword(userId, password, newPassword);
	}

}
